$(()=>{
    $("#addToCar").click(function (e){
        // layer.open({
        // 	type: 2,
        // 	title: '',
        // 	shadeClose: true,
        // 	shade: false,
        // 	maxmin: true, //开启最大化最小化按钮
        // 	area: ['420px', '460px'],
        // 	content: ctx + '/user/login'
        // 	,yes:function (index,layero){
        // 		layer.close(index);
        // 	}
        // });
        let goodName = $("#goodName").text();
        let goodId = $(this).data("good-id");//取出商品编号
        let amount = parseInt($("#quantity_1").val());//数量
        console.log('商品编号:'+goodId);
        console.log('商品数量:'+amount);
        console.log(ctx)
        layer.confirm('是否将'+goodName+'添加到购物车', {
            btn : ['确定', '取消']
            // 按钮
        }, function(index) {
            console.log("提交数据");
            let url = ctx + "/cart/add"
            console.log(url)
            $.ajax({
                url,
                method:"post",
                dataType:"json",
                data:{
                    'good.id':goodId,
                    amount,
                    redirectUrl:location.href
                },success(resp){
                    console.log(resp)
                    if (resp.success){
                        layer.msg("已添加到购物车");
                        let count = resp.cartItemCount;
                        $("#cartItemCount").text(count);
                        setTimeout(function (){
                            window.location.reload();
                        },800)

                    }

                }
                ,error(resp){
                    if (resp.status === 200){
                        console.log(ctx)
                        location.href = ctx + "/user/login";
                    }
                }
            })

            layer.close(index);
        });

    })

    let a = $(".goodPrice");
    let b = $(".goodAmount");
    let priceTotal = 0;
    // console.log(a)
    for (let i = 0;i<a.length;i++){
        priceTotal += a[i].value * b[i].value;
    }
    $("#cartTotal").text('￥'+priceTotal);
})
